#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.4.0-alpha
# date: 2025-01-09 06:25:45
split__3_v0() {
    local text=$1
    local delimiter=$2
    __AMBER_ARRAY_0=();
    local result=("${__AMBER_ARRAY_0[@]}")
     IFS="${delimiter}" read -rd '' -a result < <(printf %s "$text") ;
    __AS=$?
    __AF_split3_v0=("${result[@]}");
    return 0
}
split_lines__4_v0() {
    local text=$1
    split__3_v0 "${text}" "
";
    __AF_split3_v0__40_12=("${__AF_split3_v0[@]}");
    __AF_split_lines4_v0=("${__AF_split3_v0__40_12[@]}");
    return 0
}
trim_left__7_v0() {
    local text=$1
    __AMBER_VAL_1=$( echo "${text}" | sed -e 's/^[[:space:]]*//' );
    __AS=$?;
    __AF_trim_left7_v0="${__AMBER_VAL_1}";
    return 0
}
trim_right__8_v0() {
    local text=$1
    __AMBER_VAL_2=$( echo "${text}" | sed -e 's/[[:space:]]*$//' );
    __AS=$?;
    __AF_trim_right8_v0="${__AMBER_VAL_2}";
    return 0
}
trim__9_v0() {
    local text=$1
    trim_right__8_v0 "${text}";
    __AF_trim_right8_v0__65_22="${__AF_trim_right8_v0}";
    trim_left__7_v0 "${__AF_trim_right8_v0__65_22}";
    __AF_trim_left7_v0__65_12="${__AF_trim_left7_v0}";
    __AF_trim9_v0="${__AF_trim_left7_v0__65_12}";
    return 0
}
text_contains__14_v0() {
    local text=$1
    local phrase=$2
    __AMBER_VAL_3=$( if [[ "${text}" == *"${phrase}"* ]]; then
    echo 1
  fi );
    __AS=$?;
    local result="${__AMBER_VAL_3}"
    __AF_text_contains14_v0=$([ "_${result}" != "_1" ]; echo $?);
    return 0
}
slice__22_v0() {
    local text=$1
    local index=$2
    local length=$3
    if [ $(echo ${length} '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        __AMBER_LEN="${text}";
        length=$(echo "${#__AMBER_LEN}" '-' ${index} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')
fi
    if [ $(echo ${length} '<=' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        __AF_slice22_v0="";
        return 0
fi
    __AMBER_VAL_4=$( printf "%.${length}s" "${text:${index}}" );
    __AS=$?;
    __AF_slice22_v0="${__AMBER_VAL_4}";
    return 0
}
file_exists__33_v0() {
    local path=$1
     [ -f "${path}" ] ;
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_file_exists33_v0=0;
        return 0
fi
    __AF_file_exists33_v0=1;
    return 0
}
file_chmod__39_v0() {
    local path=$1
    local mode=$2
    file_exists__33_v0 "${path}";
    __AF_file_exists33_v0__61_8="$__AF_file_exists33_v0";
    if [ "$__AF_file_exists33_v0__61_8" != 0 ]; then
         chmod "${mode}" "${path}" ;
        __AS=$?
        __AF_file_chmod39_v0=1;
        return 0
fi
    echo "The file ${path} doesn't exist"'!'""
    __AF_file_chmod39_v0=0;
    return 0
}
is_command__93_v0() {
    local command=$1
     [ -x "$(command -v ${command})" ] ;
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_is_command93_v0=0;
        return 0
fi
    __AF_is_command93_v0=1;
    return 0
}
printf__99_v0() {
    local format=$1
    local args=("${!2}")
     args=("${format}" "${args[@]}") ;
    __AS=$?
     printf "${args[@]}" ;
    __AS=$?
}
echo_info__106_v0() {
    local message=$1
    __AMBER_ARRAY_5=("${message}");
    printf__99_v0 "\x1b[1;3;97;44m%s\x1b[0m
" __AMBER_ARRAY_5[@];
    __AF_printf99_v0__147_5="$__AF_printf99_v0";
    echo "$__AF_printf99_v0__147_5" > /dev/null 2>&1
}
echo_error__109_v0() {
    local message=$1
    local exit_code=$2
    __AMBER_ARRAY_6=("${message}");
    printf__99_v0 "\x1b[1;3;97;41m%s\x1b[0m
" __AMBER_ARRAY_6[@];
    __AF_printf99_v0__162_5="$__AF_printf99_v0";
    echo "$__AF_printf99_v0__162_5" > /dev/null 2>&1
    if [ $(echo ${exit_code} '>' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        exit ${exit_code}
fi
}
file_download__142_v0() {
    local url=$1
    local path=$2
    is_command__93_v0 "curl";
    __AF_is_command93_v0__9_9="$__AF_is_command93_v0";
    is_command__93_v0 "wget";
    __AF_is_command93_v0__12_9="$__AF_is_command93_v0";
    is_command__93_v0 "aria2c";
    __AF_is_command93_v0__15_9="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__9_9" != 0 ]; then
         curl -L -o "${path}" "${url}" ;
        __AS=$?
elif [ "$__AF_is_command93_v0__12_9" != 0 ]; then
         wget "${url}" -P "${path}" ;
        __AS=$?
elif [ "$__AF_is_command93_v0__15_9" != 0 ]; then
         aria2c "${url}" -d "${path}" ;
        __AS=$?
else
        __AF_file_download142_v0=0;
        return 0
fi
    __AF_file_download142_v0=1;
    return 0
}
__0_ROOT_DATA_PATH="./proxy-data"
__1_V2RAY_ROOT_PATH="${__0_ROOT_DATA_PATH}/v2ray"
__2_V2RAY_BIN="${__1_V2RAY_ROOT_PATH}/v2ray"
__3_V2RAYA_ROOT_PATH="${__0_ROOT_DATA_PATH}/v2raya"
__4_V2RAYA_CONFIG_PATH="${__3_V2RAYA_ROOT_PATH}/config"
__5_V2RAYA_BIN="${__3_V2RAYA_ROOT_PATH}/v2raya"
__6_V2RAYA_LOG_FILE="${__3_V2RAYA_ROOT_PATH}/v2raya.log"
get_v2ray_arch__146_v0() {
    __AMBER_VAL_7=$(uname --machine);
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_get_v2ray_arch146_v0=''
return $__AS
fi;
    local arch="${__AMBER_VAL_7}"
    if [ $([ "_${arch}" != "_x86_64" ]; echo $?) != 0 ]; then
        __AF_get_v2ray_arch146_v0="64";
        return 0
elif [ $([ "_${arch}" != "_i386" ]; echo $?) != 0 ]; then
        __AF_get_v2ray_arch146_v0="32";
        return 0
elif [ $([ "_${arch}" != "_aarch64" ]; echo $?) != 0 ]; then
        __AF_get_v2ray_arch146_v0="arm64-v8a";
        return 0
else
        echo_error__109_v0 "Unsupported architecture: ${arch}" 1;
        __AF_echo_error109_v0__29_13="$__AF_echo_error109_v0";
        echo "$__AF_echo_error109_v0__29_13" > /dev/null 2>&1
fi
}
get_v2raya_arch__147_v0() {
    __AMBER_VAL_8=$(uname --machine);
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_get_v2raya_arch147_v0=''
return $__AS
fi;
    local arch="${__AMBER_VAL_8}"
    if [ $([ "_${arch}" != "_x86_64" ]; echo $?) != 0 ]; then
        __AF_get_v2raya_arch147_v0="x64";
        return 0
elif [ $([ "_${arch}" != "_i386" ]; echo $?) != 0 ]; then
        __AF_get_v2raya_arch147_v0="x86";
        return 0
elif [ $([ "_${arch}" != "_aarch64" ]; echo $?) != 0 ]; then
        __AF_get_v2raya_arch147_v0="arm64";
        return 0
else
        echo_error__109_v0 "Unsupported architecture: ${arch}" 1;
        __AF_echo_error109_v0__47_13="$__AF_echo_error109_v0";
        echo "$__AF_echo_error109_v0__47_13" > /dev/null 2>&1
fi
}
get_v2ray_os__148_v0() {
    __AMBER_VAL_9=$(uname --operating-system);
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_get_v2ray_os148_v0=''
return $__AS
fi;
    local os="${__AMBER_VAL_9}"
    text_contains__14_v0 "${os}" "Linux";
    __AF_text_contains14_v0__55_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "Darwin";
    __AF_text_contains14_v0__58_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "Windows";
    __AF_text_contains14_v0__61_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "FreeBSD";
    __AF_text_contains14_v0__64_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "OpenBSD";
    __AF_text_contains14_v0__67_9="$__AF_text_contains14_v0";
    if [ "$__AF_text_contains14_v0__55_9" != 0 ]; then
        __AF_get_v2ray_os148_v0="linux";
        return 0
elif [ "$__AF_text_contains14_v0__58_9" != 0 ]; then
        __AF_get_v2ray_os148_v0="macos";
        return 0
elif [ "$__AF_text_contains14_v0__61_9" != 0 ]; then
        __AF_get_v2ray_os148_v0="windows";
        return 0
elif [ "$__AF_text_contains14_v0__64_9" != 0 ]; then
        __AF_get_v2ray_os148_v0="freebsd";
        return 0
elif [ "$__AF_text_contains14_v0__67_9" != 0 ]; then
        __AF_get_v2ray_os148_v0="openbsd";
        return 0
else
        echo_error__109_v0 "Unsupported operating system: ${os}" 1;
        __AF_echo_error109_v0__71_13="$__AF_echo_error109_v0";
        echo "$__AF_echo_error109_v0__71_13" > /dev/null 2>&1
fi
}
get_v2raya_os__149_v0() {
    __AMBER_VAL_10=$(uname --operating-system);
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_get_v2raya_os149_v0=''
return $__AS
fi;
    local os="${__AMBER_VAL_10}"
    text_contains__14_v0 "${os}" "Linux";
    __AF_text_contains14_v0__79_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "Darwin";
    __AF_text_contains14_v0__82_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "Windows";
    __AF_text_contains14_v0__85_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "FreeBSD";
    __AF_text_contains14_v0__88_9="$__AF_text_contains14_v0";
    text_contains__14_v0 "${os}" "OpenBSD";
    __AF_text_contains14_v0__91_9="$__AF_text_contains14_v0";
    if [ "$__AF_text_contains14_v0__79_9" != 0 ]; then
        __AF_get_v2raya_os149_v0="linux";
        return 0
elif [ "$__AF_text_contains14_v0__82_9" != 0 ]; then
        __AF_get_v2raya_os149_v0="darwin";
        return 0
elif [ "$__AF_text_contains14_v0__85_9" != 0 ]; then
        __AF_get_v2raya_os149_v0="windows";
        return 0
elif [ "$__AF_text_contains14_v0__88_9" != 0 ]; then
        __AF_get_v2raya_os149_v0="freebsd";
        return 0
elif [ "$__AF_text_contains14_v0__91_9" != 0 ]; then
        __AF_get_v2raya_os149_v0="openbsd";
        return 0
else
        echo_error__109_v0 "Unsupported operating system: ${os}" 1;
        __AF_echo_error109_v0__95_13="$__AF_echo_error109_v0";
        echo "$__AF_echo_error109_v0__95_13" > /dev/null 2>&1
fi
}
get_v2raya_version__150_v0() {
    echo_info__106_v0 "开始获取 v2raya 版本";
    __AF_echo_info106_v0__101_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__101_5" > /dev/null 2>&1
    __AMBER_VAL_11=$(curl --silent https://apt.v2raya.org/dists/v2raya/main/binary-amd64/Packages);
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_get_v2raya_version150_v0=''
return $__AS
fi;
    local version_raw_output="${__AMBER_VAL_11}"
    split_lines__4_v0 "${version_raw_output}";
    __AF_split_lines4_v0__103_21=("${__AF_split_lines4_v0[@]}");
    local raw_lines=("${__AF_split_lines4_v0__103_21[@]}")
    local found_v2raya=0
    index=0;
for line in "${raw_lines[@]}"; do
        text_contains__14_v0 "${line}" "Package: v2raya";
        __AF_text_contains14_v0__106_12="$__AF_text_contains14_v0";
        if [ "$__AF_text_contains14_v0__106_12" != 0 ]; then
            if [ ${found_v2raya} != 0 ]; then
                echo_error__109_v0 "找到多个 v2raya 包" 1;
                __AF_echo_error109_v0__108_17="$__AF_echo_error109_v0";
                echo "$__AF_echo_error109_v0__108_17" > /dev/null 2>&1
fi
            found_v2raya=1
fi
        text_contains__14_v0 "${line}" "Version: ";
        __AF_text_contains14_v0__112_29="$__AF_text_contains14_v0";
        if [ $(echo ${found_v2raya} '&&' "$__AF_text_contains14_v0__112_29" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            slice__22_v0 "${line}" 9 0;
            __AF_slice22_v0__113_27="${__AF_slice22_v0}";
            local version="${__AF_slice22_v0__113_27}"
            echo_info__106_v0 "获取的最新 v2raya 版本: ${version}";
            __AF_echo_info106_v0__114_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__114_13" > /dev/null 2>&1
            __AF_get_v2raya_version150_v0="${version}";
            return 0
fi
    (( index++ )) || true
done
    echo_error__109_v0 "未成功获取 v2raya 版本，请检查网络连接" 1;
    __AF_echo_error109_v0__118_5="$__AF_echo_error109_v0";
    echo "$__AF_echo_error109_v0__118_5" > /dev/null 2>&1
}
build_v2ray_download_url__151_v0() {
    get_v2ray_arch__146_v0 ;
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_build_v2ray_download_url151_v0=''
return $__AS
fi;
    __AF_get_v2ray_arch146_v0__122_18="${__AF_get_v2ray_arch146_v0}";
    local arch="${__AF_get_v2ray_arch146_v0__122_18}"
    get_v2ray_os__148_v0 ;
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_build_v2ray_download_url151_v0=''
return $__AS
fi;
    __AF_get_v2ray_os148_v0__123_16="${__AF_get_v2ray_os148_v0}";
    local os="${__AF_get_v2ray_os148_v0__123_16}"
    __AF_build_v2ray_download_url151_v0="https://gh-proxy.com/https://github.com/v2fly/v2ray-core/releases/latest/download/v2ray-${os}-${arch}.zip";
    return 0
}
build_v2raya_download_url__152_v0() {
    get_v2raya_arch__147_v0 ;
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_build_v2raya_download_url152_v0=''
return $__AS
fi;
    __AF_get_v2raya_arch147_v0__128_18="${__AF_get_v2raya_arch147_v0}";
    local arch="${__AF_get_v2raya_arch147_v0__128_18}"
    get_v2raya_os__149_v0 ;
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_build_v2raya_download_url152_v0=''
return $__AS
fi;
    __AF_get_v2raya_os149_v0__129_16="${__AF_get_v2raya_os149_v0}";
    local os="${__AF_get_v2raya_os149_v0__129_16}"
    get_v2raya_version__150_v0 ;
    __AS=$?;
if [ $__AS != 0 ]; then
__AF_build_v2raya_download_url152_v0=''
return $__AS
fi;
    __AF_get_v2raya_version150_v0__130_21="${__AF_get_v2raya_version150_v0}";
    local version="${__AF_get_v2raya_version150_v0__130_21}"
    if [ $([ "_${os}" != "_windows" ]; echo $?) != 0 ]; then
        __AF_build_v2raya_download_url152_v0="https://gh-proxy.com/https://github.com/v2rayA/v2rayA/releases/download/v${version}/v2raya_${os}_${arch}_${version}.exe";
        return 0
else
        __AF_build_v2raya_download_url152_v0="https://gh-proxy.com/https://github.com/v2rayA/v2rayA/releases/download/v${version}/v2raya_${os}_${arch}_${version}";
        return 0
fi
}
check_v2ray_availability__153_v0() {
    is_command__93_v0 "v2ray";
    __AF_is_command93_v0__142_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__142_8" != 0 ]; then
        __AF_check_v2ray_availability153_v0="Command";
        return 0
fi
    file_exists__33_v0 "${__2_V2RAY_BIN}";
    __AF_file_exists33_v0__146_12="$__AF_file_exists33_v0";
    if [ $(echo  '!' "$__AF_file_exists33_v0__146_12" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        __AF_check_v2ray_availability153_v0="NotFound";
        return 0
fi
    test -s ${__2_V2RAY_BIN};
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_check_v2ray_availability153_v0="NotFound";
        return 0
fi
    __AMBER_VAL_12=$(${__2_V2RAY_BIN} version);
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_check_v2ray_availability153_v0="NotFound";
        return 0
fi;
    local version="${__AMBER_VAL_12}"
    trim__9_v0 "${version}";
    __AF_trim9_v0__157_8="${__AF_trim9_v0}";
    if [ $([ "_${__AF_trim9_v0__157_8}" != "_" ]; echo $?) != 0 ]; then
        __AF_check_v2ray_availability153_v0="NotFound";
        return 0
fi
    __AF_check_v2ray_availability153_v0="Local";
    return 0
}
check_v2raya_availability__154_v0() {
    is_command__93_v0 "v2raya";
    __AF_is_command93_v0__165_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__165_8" != 0 ]; then
        __AF_check_v2raya_availability154_v0="Command";
        return 0
fi
    file_exists__33_v0 "${__5_V2RAYA_BIN}";
    __AF_file_exists33_v0__169_12="$__AF_file_exists33_v0";
    if [ $(echo  '!' "$__AF_file_exists33_v0__169_12" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        __AF_check_v2raya_availability154_v0="NotFound";
        return 0
fi
    test -s ${__5_V2RAYA_BIN};
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_check_v2raya_availability154_v0="NotFound";
        return 0
fi
    __AMBER_VAL_13=$(${__5_V2RAYA_BIN} --version);
    __AS=$?;
if [ $__AS != 0 ]; then
        __AF_check_v2raya_availability154_v0="NotFound";
        return 0
fi;
    local version="${__AMBER_VAL_13}"
    trim__9_v0 "${version}";
    __AF_trim9_v0__180_8="${__AF_trim9_v0}";
    if [ $([ "_${__AF_trim9_v0__180_8}" != "_" ]; echo $?) != 0 ]; then
        __AF_check_v2raya_availability154_v0="NotFound";
        return 0
fi
    __AF_check_v2raya_availability154_v0="Local";
    return 0
}
extract_zip__155_v0() {
    local zip_file=$1
    local target_dir=$2
    is_command__93_v0 "unzip";
    __AF_is_command93_v0__188_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__188_8" != 0 ]; then
        unzip -o ${zip_file} -d ${target_dir};
        __AS=$?;
if [ $__AS != 0 ]; then
__AF_extract_zip155_v0=''
return $__AS
fi
        __AF_extract_zip155_v0=1;
        return 0
fi
    is_command__93_v0 "7z";
    __AF_is_command93_v0__192_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__192_8" != 0 ]; then
        7z x ${zip_file} -o${target_dir};
        __AS=$?;
if [ $__AS != 0 ]; then
__AF_extract_zip155_v0=''
return $__AS
fi
        __AF_extract_zip155_v0=1;
        return 0
fi
    is_command__93_v0 "bsdtar";
    __AF_is_command93_v0__196_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__196_8" != 0 ]; then
        bsdtar -xf ${zip_file} -C ${target_dir};
        __AS=$?;
if [ $__AS != 0 ]; then
__AF_extract_zip155_v0=''
return $__AS
fi
        __AF_extract_zip155_v0=1;
        return 0
fi
    is_command__93_v0 "python";
    __AF_is_command93_v0__200_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__200_8" != 0 ]; then
        python -m zipfile -e ${zip_file} ${target_dir};
        __AS=$?;
if [ $__AS != 0 ]; then
__AF_extract_zip155_v0=''
return $__AS
fi
        __AF_extract_zip155_v0=1;
        return 0
fi
    echo_error__109_v0 "无法解压 zip 文件，请安装 unzip, 7z, bsdtar 或 python" 1;
    __AF_echo_error109_v0__204_5="$__AF_echo_error109_v0";
    echo "$__AF_echo_error109_v0__204_5" > /dev/null 2>&1
}
check_ipv4_local_port_availability__156_v0() {
    local port=$1
    is_command__93_v0 "nc";
    __AF_is_command93_v0__208_8="$__AF_is_command93_v0";
    if [ "$__AF_is_command93_v0__208_8" != 0 ]; then
        nc -z 127.0.0.1 ${port};
        __AS=$?;
if [ $__AS != 0 ]; then
            __AF_check_ipv4_local_port_availability156_v0=1;
            return 0
fi
        __AF_check_ipv4_local_port_availability156_v0=0;
        return 0
fi
    echo_info__106_v0 "无法检查端口占用情况，请安装 nc。将尝试无视端口占用情况";
    __AF_echo_info106_v0__214_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__214_5" > /dev/null 2>&1
    __AF_check_ipv4_local_port_availability156_v0=1;
    return 0
}
declare -r cmd_args=("$0" "$@")
    i=0;
for arg in "${cmd_args[@]}"; do
        if [ $(echo $(echo $(echo $([ "_${arg}" != "_help" ]; echo $?) '||' $([ "_${arg}" != "_-h" ]; echo $?) | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') '||' $([ "_${arg}" != "_--help" ]; echo $?) | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') '||' $([ "_${arg}" != "_-?" ]; echo $?) | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            echo "用法: ./proxy.sh [help|stop|clean|status]"
            echo "选项:"
            echo "  不加参数: 安装并启动 v2raya 服务"
            echo "  help: 显示此帮助信息"
            echo "  stop: 停止 v2raya 服务"
            echo "  clean: 清理所有本脚本产生的文件"
            echo "  status: 检查 v2ray 和 v2raya 状态"
            exit 0
fi
        if [ $([ "_${arg}" != "_stop" ]; echo $?) != 0 ]; then
            pkill "v2raya";
            __AS=$?;
if [ $__AS != 0 ]; then
                echo_error__109_v0 "停止 v2raya 服务失败，一般是因为服务未启动" 1;
                __AF_echo_error109_v0__232_17="$__AF_echo_error109_v0";
                echo "$__AF_echo_error109_v0__232_17" > /dev/null 2>&1
fi
            echo_info__106_v0 "停止 v2raya 服务成功";
            __AF_echo_info106_v0__234_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__234_13" > /dev/null 2>&1
            exit 0
fi
        if [ $([ "_${arg}" != "_clean" ]; echo $?) != 0 ]; then
            rm -rf ${__0_ROOT_DATA_PATH};
            __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
            echo_info__106_v0 "清理完成";
            __AF_echo_info106_v0__239_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__239_13" > /dev/null 2>&1
            exit 0
fi
        if [ $([ "_${arg}" != "_status" ]; echo $?) != 0 ]; then
            echo_info__106_v0 "检查 v2ray 和 v2raya 运行状态…";
            __AF_echo_info106_v0__243_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__243_13" > /dev/null 2>&1
            ps aux | grep v2ray | grep -v grep;
            __AS=$?
            echo_info__106_v0 "检查 v2ray 和 v2raya 安装状态…";
            __AF_echo_info106_v0__245_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__245_13" > /dev/null 2>&1
            check_v2ray_availability__153_v0 ;
            __AF_check_v2ray_availability153_v0__246_35="${__AF_check_v2ray_availability153_v0}";
            echo_info__106_v0 "v2ray 状态: ${__AF_check_v2ray_availability153_v0__246_35}";
            __AF_echo_info106_v0__246_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__246_13" > /dev/null 2>&1
            check_v2raya_availability__154_v0 ;
            __AF_check_v2raya_availability154_v0__247_36="${__AF_check_v2raya_availability154_v0}";
            echo_info__106_v0 "v2raya 状态: ${__AF_check_v2raya_availability154_v0__247_36}";
            __AF_echo_info106_v0__247_13="$__AF_echo_info106_v0";
            echo "$__AF_echo_info106_v0__247_13" > /dev/null 2>&1
            exit 0
fi
    (( i++ )) || true
done
    check_v2ray_availability__153_v0 ;
    __AF_check_v2ray_availability153_v0__252_22="${__AF_check_v2ray_availability153_v0}";
    cur_status="${__AF_check_v2ray_availability153_v0__252_22}"
    v2ray_path="${__2_V2RAY_BIN}"
    if [ $([ "_${cur_status}" != "_NotFound" ]; echo $?) != 0 ]; then
        build_v2ray_download_url__151_v0 ;
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
        __AF_build_v2ray_download_url151_v0__256_23="${__AF_build_v2ray_download_url151_v0}";
        url="${__AF_build_v2ray_download_url151_v0__256_23}"
        echo_info__106_v0 "从地址 ${url} 下载 v2ray…";
        __AF_echo_info106_v0__257_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__257_13" > /dev/null 2>&1
        file_download__142_v0 "${url}" "v2ray.zip";
        __AF_file_download142_v0__258_20="$__AF_file_download142_v0";
        if [ $(echo  '!' "$__AF_file_download142_v0__258_20" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            echo_error__109_v0 "下载失败" 1;
            __AF_echo_error109_v0__259_17="$__AF_echo_error109_v0";
            echo "$__AF_echo_error109_v0__259_17" > /dev/null 2>&1
fi
        echo_info__106_v0 "下载成功";
        __AF_echo_info106_v0__261_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__261_13" > /dev/null 2>&1
        mkdir --parents ${__1_V2RAY_ROOT_PATH};
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
        extract_zip__155_v0 "v2ray.zip" "${__1_V2RAY_ROOT_PATH}";
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
        __AF_extract_zip155_v0__263_13="$__AF_extract_zip155_v0";
        echo "$__AF_extract_zip155_v0__263_13" > /dev/null 2>&1
        rm v2ray.zip;
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
elif [ $([ "_${cur_status}" != "_Command" ]; echo $?) != 0 ]; then
        echo_info__106_v0 "已找到 v2ray 命令，跳过下载";
        __AF_echo_info106_v0__267_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__267_13" > /dev/null 2>&1
        v2ray_path="v2ray"
elif [ $([ "_${cur_status}" != "_Local" ]; echo $?) != 0 ]; then
        echo_info__106_v0 "已找到本地 v2ray 可执行文件，跳过下载";
        __AF_echo_info106_v0__271_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__271_13" > /dev/null 2>&1
fi
    check_v2raya_availability__154_v0 ;
    __AF_check_v2raya_availability154_v0__275_22="${__AF_check_v2raya_availability154_v0}";
    cur_status="${__AF_check_v2raya_availability154_v0__275_22}"
    v2raya_path="${__5_V2RAYA_BIN}"
    if [ $([ "_${cur_status}" != "_NotFound" ]; echo $?) != 0 ]; then
        build_v2raya_download_url__152_v0 ;
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
        __AF_build_v2raya_download_url152_v0__280_23="${__AF_build_v2raya_download_url152_v0}";
        url="${__AF_build_v2raya_download_url152_v0__280_23}"
        echo_info__106_v0 "从地址 ${url} 下载 v2raya…";
        __AF_echo_info106_v0__281_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__281_13" > /dev/null 2>&1
        mkdir --parents ${__3_V2RAYA_ROOT_PATH};
        __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
        file_download__142_v0 "${url}" "${__5_V2RAYA_BIN}";
        __AF_file_download142_v0__283_20="$__AF_file_download142_v0";
        if [ $(echo  '!' "$__AF_file_download142_v0__283_20" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            echo_error__109_v0 "下载失败" 1;
            __AF_echo_error109_v0__284_17="$__AF_echo_error109_v0";
            echo "$__AF_echo_error109_v0__284_17" > /dev/null 2>&1
fi
        echo_info__106_v0 "下载成功";
        __AF_echo_info106_v0__286_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__286_13" > /dev/null 2>&1
        file_chmod__39_v0 "${__5_V2RAYA_BIN}" "755";
        __AF_file_chmod39_v0__287_13="$__AF_file_chmod39_v0";
        echo "$__AF_file_chmod39_v0__287_13" > /dev/null 2>&1
elif [ $([ "_${cur_status}" != "_Command" ]; echo $?) != 0 ]; then
        echo_info__106_v0 "已找到 v2raya 命令，跳过下载";
        __AF_echo_info106_v0__290_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__290_13" > /dev/null 2>&1
        v2raya_path="v2raya"
elif [ $([ "_${cur_status}" != "_Local" ]; echo $?) != 0 ]; then
        echo_info__106_v0 "已找到本地 v2raya 可执行文件，跳过下载";
        __AF_echo_info106_v0__294_13="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__294_13" > /dev/null 2>&1
fi
    __AMBER_VAL_14=$(${v2ray_path} version);
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
    version="${__AMBER_VAL_14}"
    echo_info__106_v0 "当前 v2ray 版本: ${version}";
    __AF_echo_info106_v0__299_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__299_5" > /dev/null 2>&1
    __AMBER_VAL_15=$(${v2raya_path} --version);
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
    version="${__AMBER_VAL_15}"
    echo_info__106_v0 "当前 v2raya 版本: ${version}";
    __AF_echo_info106_v0__301_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__301_5" > /dev/null 2>&1
    final_port=0
    for port in $(seq 2017 2066); do
        check_ipv4_local_port_availability__156_v0 ${port};
        __AF_check_ipv4_local_port_availability156_v0__306_12="$__AF_check_ipv4_local_port_availability156_v0";
        if [ "$__AF_check_ipv4_local_port_availability156_v0__306_12" != 0 ]; then
            final_port=${port}
            break
fi
        echo_info__106_v0 "端口 ${port} 已被占用，请确认 v2raya 服务已停止！正在尝试下一个端口 $(echo ${port} '+' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')…";
        __AF_echo_info106_v0__310_9="$__AF_echo_info106_v0";
        echo "$__AF_echo_info106_v0__310_9" > /dev/null 2>&1
done
    if [ $(echo ${final_port} '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
        echo_error__109_v0 "无法找到可用端口，请检查端口占用情况" 1;
        __AF_echo_error109_v0__313_9="$__AF_echo_error109_v0";
        echo "$__AF_echo_error109_v0__313_9" > /dev/null 2>&1
fi
    echo_info__106_v0 "选定的 v2raya 前端端口: ${final_port}";
    __AF_echo_info106_v0__315_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__315_5" > /dev/null 2>&1
    echo_info__106_v0 "启动 v2raya 服务…";
    __AF_echo_info106_v0__317_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__317_5" > /dev/null 2>&1
    mkdir --parents ${__4_V2RAYA_CONFIG_PATH};
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
    __AMBER_VAL_16=$(realpath ${v2ray_path});
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
    real_v2ray_path="${__AMBER_VAL_16}"
    __AMBER_VAL_17=$(realpath ${__1_V2RAY_ROOT_PATH});
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
    real_v2ray_root_path="${__AMBER_VAL_17}"
    __AMBER_VAL_18=$(realpath ${__4_V2RAYA_CONFIG_PATH});
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi;
    real_v2raya_config_path="${__AMBER_VAL_18}"
    service_command="V2RAYA_ADDRESS=0.0.0.0:${final_port} V2RAYA_CONFIG=${real_v2raya_config_path} V2RAYA_LOG_FILE=${__6_V2RAYA_LOG_FILE} V2RAYA_V2RAY_BIN=${real_v2ray_path} V2RAYA_V2RAY_ASSETSDIR=${real_v2ray_root_path} nohup ${v2raya_path} --lite </dev/null >/dev/null 2>&1 &"
    bash -c "${service_command}";
    __AS=$?;
if [ $__AS != 0 ]; then

exit $__AS
fi
    echo_info__106_v0 "v2raya 服务已启动，请访问 http://127.0.0.1:${final_port} 查看";
    __AF_echo_info106_v0__324_5="$__AF_echo_info106_v0";
    echo "$__AF_echo_info106_v0__324_5" > /dev/null 2>&1
